package com.cjj.common.utils;

import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;

/**
 * @author DoyuTu
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ObjectUtil {

    /**
     * 验证对象属性是否全为<code>null</code>或者空字符串
     *
     * @param obj 对象
     * @return true：对象属性全为空
     */
    public static boolean isEmptyObject(Object obj) {
        try {
            for (Field field : obj.getClass().getDeclaredFields()) {
                ReflectionUtils.makeAccessible(field);
                Object o = field.get(obj);
                if (o != null && StrUtil.isNotBlank(o.toString())) {
                    return false;
                }
            }
        } catch (IllegalAccessException e) {
            log.error("验证空对象失败", e);
            return false;
        }
        return true;
    }

}
